//
//  AppDelegate.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/1.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

var textView = UITextView()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UIAlertViewDelegate {
    
    var window: UIWindow?
    
    let tool = WXTool()
        let qqTool = QQTool()

    
    lazy var tencentOAuth = TencentOAuth()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         //创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        //启动界面加导航栏
        let nav : UINavigationController = UINavigationController(rootViewController: AlipayViewController())
        
        
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
       //启动主界面
//        setMainBar()
        
    
        //测试用
        textView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 35)
        textView.isUserInteractionEnabled = false
        textView.font = UIFont.systemFont(ofSize: 8)
        self.window?.addSubview(textView)
        return true
    }
}

// MARK:- 若已退出，重新生命，重新请求加载
extension AppDelegate {
    func applicationWillResignActive(_ application: UIApplication) {
        
        
        //取出本地用户名。密码
        guard let pass = localSave.object(forKey: localPass) as? String else {
            return
        }
        
        guard let uname = localSave.object(forKey: localName) as? String else {
            return
        }

        LoginModel.shared.loginWithLocalInfo(tfNum: uname, tfPass: pass) { (result) in
            
        }
        
    }
}


// MARK:- 设置主控制器
extension AppDelegate {
    func setMainBar() -> Void {
        // 创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        window?.makeKeyAndVisible()
    

        let customtabbar = MainViewController()
        window?.rootViewController = customtabbar
        
        setQQ()
        setWX()

        // 设置全局颜色
        UITabBar.appearance().tintColor = UIColor.orange
        UINavigationBar.appearance().tintColor = UIColor.orange
        
        //测试红。。。。
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
    }
}

// MARK:- 第三方介入
extension AppDelegate {
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let wxUrl = String(format: "%@", arguments: [WXPatient_App_ID])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
                        return WXApi.handleOpen(url, delegate: tool)
//            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
            //微信支付
//        }
        
//        else if url.host == "safepay" {
//            // 支付跳转支付宝钱包进行支付，处理支付结果
//            
//            AlipaySDK.defaultService().processOrder(withPaymentResult: url, standbyCallback: {[weak self] (resultDic) in
//                
//                
//                var data = NSDictionary()
//                
//                if let resultStatus = resultDic?["resultStatus"] as? String {
//                    
//                    if resultStatus == "9000"{
//                        data = ["re" : "支付成功"]
//                        
//                    }else if resultStatus == "8000"{
//                        data = ["re" : "正在处理中"]
//                        
//                    }else if resultStatus == "4000"{
//                        data = ["re" : "订单支付失败"]
//                        
//                    }else if resultStatus == "6001" {
//                        data = ["re" : "用户中途取消"]
//                        
//                    } else if resultStatus == "6002" {
//                        data = ["re" : "网络连接出错"]
//                    }
//                }
//                
//                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "123"), object: self, userInfo: data as? [AnyHashable : Any])
//                
//            })
        
        } else if url.scheme == WXPatient_App_ID {
            return WXApi.handleOpen(url, delegate: tool)
        }
        return true;
    
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        
        let wxUrl = String(format: "%@", arguments: [WXPatient_App_ID])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
                        return WXApi.handleOpen(url, delegate: tool)
//            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
        }
        return true;
    }
    
    // MARK:- 微信分享
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        
        let wxUrl = String(format: "%@", arguments: [WXPatient_App_ID])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
                        return WXApi.handleOpen(url, delegate: tool)
//            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
        }
        return true;
    }
}

// MARK:- 微信
extension AppDelegate {
    
    func setWX() -> Void {
        WXApi.registerApp(WXPatient_App_ID)
        
    }
}

// MARK:- QQ
extension AppDelegate {
    func setQQ() -> Void {
        tencentOAuth = TencentOAuth(appId: QQAppID, andDelegate: qqTool)
    }
}




